-- UIGDInput1
-- Created by cheny3 Mar/07/2016
-- GD界面输入界面1（两个输入框/两个按钮）

UIGDInput1 = class(" UIGDInput1", function()
    return cc.Layer:create();
end);

function UIGDInput1.create(gdType)
    return UIGDInput1.new(gdType);
end

-- 只有一个输入框的类型
local oneItemTypes = {"open_dungeon", "hero", "dp"};

-- 构造函数
function UIGDInput1:ctor(gdType)
    self:setName("UIGDInput1");

    -- 初始化
    local node = cc.CSLoader:createNode("layout/GD/GDInput1.csb");
    self.node = node;
    self:addChild(node);

    self.gdType = gdType;

    -- 设置层级s
    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 100001);

    -- 绘制
    self:redraw(node);

    -- 注册点击事件
    self:registerTouchEvent();

    -- 适配
    self:resize(node);
end

-- 绘制
function UIGDInput1:redraw()
    local bg1 = findChildByName(self.node, "CT/bg1");
    local bg2 = findChildByName(self.node, "CT/bg2");
    local edit1 = findChildByName(self.node, "CT/edit1");
    local edit2 = findChildByName(self.node, "CT/edit2");
    -- 创建两个新的editBox
    local editEx1 = ccui.EditBox:create(edit1:getContentSize(), "images/ui/login/1.png");
    editEx1:setAnchorPoint(cc.p(0.5, 0.5));
    editEx1:setPosition(edit1:getPosition());
    local editEx2 = ccui.EditBox:create(edit2:getContentSize(), "images/ui/login/1.png");
    editEx2:setAnchorPoint(cc.p(0.5, 0.5));
    editEx2:setPosition(edit2:getPosition());

    editEx1:setFontName(getLocFontName());
    editEx1:setFontSize(TextStyleM.TEXT_SIZE_TINY);
    editEx1:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
    editEx1:setMaxLength(16);
    editEx1:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);

    editEx2:setFontName(getLocFontName());
    editEx2:setFontSize(TextStyleM.TEXT_SIZE_TINY);
    editEx2:setPlaceholderFontColor(TextStyleM.TEXT_COLOR_NORMAL);
    editEx2:setMaxLength(16);
    editEx2:setReturnType(cc.KEYBOARD_RETURNTYPE_DONE);

    self.editEx1 = editEx1;
    self.editEx2 = editEx2;

    edit1:getParent():addChild(editEx1);
    edit2:getParent():addChild(editEx2);

    edit1:setVisible(false);
    edit2:setVisible(false);

    bg1:setVisible(true);
    self.editEx1:setVisible(true);
    if table.indexOf(oneItemTypes, self.gdType) >= 0 then
        -- 只显示一个的输入的
        bg2:setVisible(false);
        self.editEx2:setVisible(false);
    else
        bg2:setVisible(true);
        self.editEx2:setVisible(true);
    end

    -- 设置输入框默认文字
    self:setPlaceHolder();
end

-- 设置输入框默认文字
function UIGDInput1:setPlaceHolder()
    if self.gdType == "open_dungeon" then
        -- 开启地图
        self.editEx1:setPlaceHolder(getLocStr("gd_add_map"));
        self.editEx1:setText("");
    elseif self.gdType == "dungeon_progress" then
        -- 设置地牢进度
        self.editEx1:setPlaceHolder(getLocStr("gd_change_dungeon_progress_1"));
        self.editEx1:setText("");
        self.editEx2:setPlaceHolder(getLocStr("gd_change_dungeon_progress_2"));
        self.editEx2:setText("");
    elseif self.gdType == "hero" then
        -- 复制英雄
        self.editEx1:setPlaceHolder(getLocStr("gd_add_pet"));
        self.editEx1:setText("");
    elseif self.gdType == "copy_item" then
        -- 复制物品
        self.editEx1:setPlaceHolder(getLocStr("gd_add_item_1"));
        self.editEx1:setText("");
        self.editEx2:setPlaceHolder(getLocStr("gd_add_item_2"));
        self.editEx2:setText("");
    elseif self.gdType == "dp" then
        -- 设置DP
        self.editEx1:setPlaceHolder(getLocStr("gd_set_dp"));
        self.editEx1:setText("");
    elseif self.gdType == "enter_dungeon" then
        -- 进入迷宫
        self.editEx1:setPlaceHolder(getLocStr("gd_change_dungeon_progress_1"));
        self.editEx1:setText("");
        self.editEx2:setPlaceHolder(getLocStr("gd_change_dungeon_progress_2"));
        self.editEx2:setText("");
    else
        cclog("error debug type input!");
    end
end

-- 确认事件
function UIGDInput1:okClickEvent()
    if self.gdType == "open_dungeon" then
        -- 开启地图
        local map_id = tonumber(self.editEx1:getText());
        GDM.add_map(map_id);
    elseif self.gdType == "dungeon_progress" then
        -- 迷宫进度
        local area_id = tonumber(self.editEx1:getText());
        local curr = tonumber(self.editEx2:getText());
        GDM.change_dungeon_progress(area_id, curr);
    elseif self.gdType == "hero" then
        -- 复制英雄
        local petNameList = string.explode(self.editEx1:getText(), "|");
        for _, petName in pairs(petNameList) do
            local petId = tonumber(petName);
            if petId == nil or petId == 0 then
                petId = PetM.queryPetId(petName);
            end
            GDM.add_pet(petId);
        end
    elseif self.gdType == "copy_item" then
        -- 复制物品
        local count = tonumber(self.editEx2:getText());
        -- count忘记设置的时，cout默认为1
        count = iif(count == 0, 1, count);
        local itemNameList = string.explode(self.editEx1:getText(), "|");
        for _, itemName in pairs(itemNameList) do
            local itemId = tonumber(itemName);
            if itemId == nil or itemId == 0 then
                local item_ids = ItemM.getItemsIdByName(itemName);
                for key, value in pairs(item_ids) do
                    local id = value;
                    GDM.add_item(id, count);
                end
            else
                GDM.add_item(itemId, count);
            end
        end
    elseif self.gdType == "dp" then
        -- 设置DP
        local needDp = tonumber(self.editEx1:getText());
        needDp = needDp or 0;
        GDM.change_achievement_progress(needDp);
    elseif self.gdType == "enter_dungeon" then
        -- 直接进入迷宫
        local dungeonId = tonumber(self.editEx1:getText());
        local layer = tonumber(self.editEx2:getText());
        GDM.goDungeon(dungeonId, layer);
    else
        cclog("error debug type input!");
    end
end

-- 注册点击事件
function UIGDInput1:registerTouchEvent()
    -- 确定按钮
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:okClickEvent();
            UIMgr.getCurrentScene():removeFormByName("UIGDInput1");
        end
    end
    btnOk:addTouchEventListener(onOkClick);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk:setTitleText(getLocStr("confirm"));

    -- 取消
    local btnCancel = findChildByName(self.node, "CT/btn_cancel");
    local function onCancelClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIGDInput1");
        end
    end
    btnCancel:addTouchEventListener(onCancelClick);
    TextStyleM.setTextStyle(btnCancel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnCancel:setTitleText(getLocStr("cancel"));
end

-- 适配
function UIGDInput1:resize(node)
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");
end